package com.github.baichuan.application.scaffold.controller;

import com.github.baichuan.application.scaffold.domain.organization.OrgService;
import com.github.baichuan.application.scaffold.dto.org.OrgForm;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.servlet.Restful;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "机构")
@RequestMapping("/org")
public class OrgController {
    @Autowired
    private OrgService orgService;

    @PostMapping
    @Operation(summary="添加")
    public ResponseEntity<Result> add(@RequestHeader("x-application-code") @Parameter(hidden = true) String application, @RequestBody @Valid OrgForm form) {
        Result result = orgService.add(application, form);
        return Restful.created(result);
    }

    @PutMapping("/{uid}")
    @Operation(summary="修改")
    public ResponseEntity<Result> edit(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                       @PathVariable("uid") String uid, @RequestBody @Valid OrgForm form) {
        Result result = orgService.edit(application, uid, form);
        return Restful.response(result);
    }

    @DeleteMapping("/{uid}")
    @Operation(summary="删除")
    public ResponseEntity<Result> delete(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                         @PathVariable("uid") String uid) {
        Result result = orgService.remove(application, uid);
        return Restful.deleted(result);
    }

//    @GetMapping("/tree")
//    @Operation(summary="获取平台下的机构树")
//    public ResponseEntity singleOrg(@RequestHeader("x-application-code") @Parameter(hidden = true) String application) {
//        List<Org> orgs = orgService.getByApplicationCode(application);
//        List<OrgNode> nodes = orgNodeMapper.mapToTree(orgs);
//        return ResponseEntity.ok(nodes);
//    }
}
